package org.consoleTesting;

import java.util.Scanner;

import org.testing.model.AuthSystem;
import org.testing.model.User;

public class ProgramMenu {
	public final static String STATISTICS_ENTRY = "s";
	public final static String TEST_ENTRY = "t";
	public final static String LOGOUT_ENTRY = "l";
	public final static String EXIT_ENTRY = "e";
	private OnUserMenuKeyListener listener;
	private Scanner scanner = new Scanner(System.in);
	
	private void printMenu() {
		System.out.println(ProgramMenu.STATISTICS_ENTRY + " - to view statistics");
		System.out.println(ProgramMenu.TEST_ENTRY + " - to start new test");
		System.out.println(ProgramMenu.LOGOUT_ENTRY + " - to log out from system");
		System.out.println(ProgramMenu.EXIT_ENTRY + " - exit from program");
	}
	
	public void setOnUserMenuKeyListener(OnUserMenuKeyListener listener) {
		this.listener = listener;
	}

	public User login() {
		User commonUser = null;
		while(commonUser == null) {
			System.out.println("Please, enter your name");
			commonUser = AuthSystem.userLogin(scanner.nextLine());
			if (commonUser == null) {
				commonUser = signUp();
			}
		}
		return commonUser;		
	}
	
	private User signUp() {
		System.out.println("You not registered,  do you want to sing up?");
		System.out.println("\"r\" to register or \"n\" for no ");
		String input = scanner.nextLine();
		if (input.equals("r")) {
			System.out.println("Enter you name");
			String login = scanner.nextLine();
			AuthSystem.singUp(login);
			System.out.println("Thank you for registering");
			return AuthSystem.userLogin(login);
		} 
		return null;
	}

	public void mainMenu() {
		ItemSelectEvent event = null;
		while (true) {
			this.printMenu();
			event = new ItemSelectEvent(scanner.nextLine());
			listener.OnChooseItem(event);
		}
	}
}
